#include <iostream>
#include <engine/componentfactory.hpp>
#include <tinyxml/tinyxml.h>

#include <engine/error.hpp>
#include <engine/managedcontainer.hpp>
#include <boost/shared_ptr.hpp>

using namespace std;
using namespace CGE;

int main( int argc, char** argv )
{
	
	//	Obligatory
	cout << "ROGUELIKE~!" << endl;
	boost::shared_ptr<ComponentFactory> fact  ( new ComponentFactory() );
	
	//	Load the template specifications
	fact->loadTemplates ( "../data/components.xml" );
	//	Try the managed container with a std::vector<int>
	ManagedContainer<Component, std::list> vec;
	try
	{
		//	Create a test entity
		ptr_Entity ent (new Entity());
		
		ptr_Component c1 (fact->createComponent ( "base", ent ));
		c1->setAttribute<string> ( "testattribute", "Hello world, this rocks!" );
		c1->setAttribute<int> ( "intattribute", 42 );
		{
			
			ptr_Component c2 (fact->createComponent ( "base", ent ));
		
			
			vec.push_back ( c1 );
			vec.push_back ( c2 );

			ManagedContainer<Component, std::list>::iterator iter = vec.begin();
			ptr_Component v1 = iter->lock();
			iter++;
			ptr_Component v2 = iter->lock();
		
			cout << "The first elements of the managed list are " << v1->name() << " and " << v2->name() << endl;
			cout << "Container size is " << vec.size() << endl;
			cout << "Component " << v1->name() << ".attribute[intattribute] == " << ent->getComponent ( "baseComponent" )->getAttribute<int> ( "intattribute" ) << endl;
		}
		cout << "Container size is " << vec.size() << endl;
		//	Create the new component
		ent->addComponent ( fact->createComponent ( "base", ent ));
	}
	catch ( Error& e )
	{
		cout << e.what() << endl;
	}
	catch ( ... )
	{
		cout << "Caught unexpected error." << endl;
	}
	cout << "Container size is " << vec.size() << endl;
	return 0;
}
